/*
 * ConfiguracionBanners.java
 *
 * Created on 14 de mayo de 2007, 17:25
 */

package despachador.administracion;

import excepciones.AvwException;
import gestores.administracion.GestorBanners;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Administrador
 * @version
 */
public class ConfiguracionBanners extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String accion = "";
        String artSeleccionado[] = null;
        String destino ="";
        if (request.getParameter("opcion") != null){
            accion = request.getParameter("opcion");
        }
        try {
            if(accion.equals("insertarArticulos"))
            if (request.getParameter("artSeleccionado") != null){
                String camCodigo = request.getParameter("camCodigo");
                String banCodigo = request.getParameter("banCodigo");
                ArrayList<String> articulos = new ArrayList<String>();
                artSeleccionado = request.getParameterValues("artSeleccionado");
                int i = 0;
                while(i != artSeleccionado.length){
                    String artCodigo = artSeleccionado[i];
                    articulos.add(artCodigo);

                    i++;
                }

                    GestorBanners.insertarArticulosBanners(camCodigo, banCodigo, articulos);

            }

            
            if (accion.equals("eliminar")){

                String banCodigo = request.getParameter("banCodigo");
                String artCodigo = request.getParameter("artCodigo");
                GestorBanners.eliminarArticulosBanners(banCodigo, artCodigo);

            }
           
            
        } catch (NullPointerException ex) {
                ex.printStackTrace();
        } catch (AvwException ex) {
            ex.printStackTrace();
        }
        armarPagina(request, response);
        
    }
    
    private void armarPagina(HttpServletRequest request, 
            HttpServletResponse response) throws ServletException, IOException {
        
        // Especificamos que pagina se coloca en cada parte del layout
        request.setAttribute("1", "/vistas/administracion/encabezado.jsp");
        request.setAttribute("2", "/vistas/administracion/menu.jsp");
        request.setAttribute("3", "/vistas/administracion/configuracionBanners.jsp" );
        request.setAttribute("4", "/vistas/administracion/pie.jsp");
        
        dispatch(request, response, "/compositor/administracion/trescuerpos.jsp");
    }
    
    
    
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
// </editor-fold>
    
    /**
     * dispatcher method
     */
    protected void dispatch(HttpServletRequest request,
            HttpServletResponse response, String page)
            throws javax.servlet.ServletException,
            java.io.IOException {
        RequestDispatcher dispatcher =
                getServletContext().
                getRequestDispatcher(page);
        dispatcher.forward(request, response);
    }
}
